import Mock from 'mockjs'

const Random = Mock.Random

/**
 * 生成报销列表数据
 */
const generateReimbursementList = (query) => {
  const total = 23
  const pageSize = parseInt(query.pageSize) || 10
  const currentPage = parseInt(query.currentPage) || 1
  
  const list = []
  for (let i = 0; i < pageSize; i++) {
    const id = 'EXP' + (10000 + i + (currentPage - 1) * pageSize).toString()
    if ((currentPage - 1) * pageSize + i < total) {
      list.push({
        id,
        title: Random.ctitle(5, 10) + '报销',
        amount: Random.float(50, 5000, 2, 2),
        status: Random.pick(['pending', 'approved', 'rejected']),
        type: Random.pick(['travel', 'office', 'meal', 'other']),
        department: Random.pick(['内科', '外科', '儿科', '妇产科', '急诊科']),
        applyDate: Random.date('yyyy-MM-dd'),
        updatedDate: Random.date('yyyy-MM-dd'),
        applicant: Random.cname()
      })
    }
  }
  
  return {
    code: 200,
    message: 'success',
    data: {
      list,
      pagination: {
        total,
        pageSize,
        currentPage
      }
    }
  }
}

/**
 * 生成报销详情数据
 */
const generateReimbursementDetail = (id) => {
  const items = []
  const itemCount = Random.integer(1, 5)
  
  for (let i = 0; i < itemCount; i++) {
    items.push({
      id: i + 1,
      description: Random.ctitle(5, 15),
      amount: Random.float(50, 2000, 2, 2),
      date: Random.date('yyyy-MM-dd')
    })
  }
  
  const total = items.reduce((sum, item) => sum + item.amount, 0).toFixed(2)
  
  return {
    code: 200,
    message: 'success',
    data: {
      id,
      title: Random.ctitle(5, 10) + '报销',
      amount: total,
      status: Random.pick(['pending', 'approved', 'rejected']),
      type: Random.pick(['travel', 'office', 'meal', 'other']),
      department: Random.pick(['内科', '外科', '儿科', '妇产科', '急诊科']),
      applyDate: Random.date('yyyy-MM-dd'),
      updatedDate: Random.date('yyyy-MM-dd'),
      applicant: Random.cname(),
      remark: Random.cparagraph(1, 3),
      items,
      approvalHistory: Random.pick([[], [
        {
          operator: Random.cname(),
          action: 'submit',
          date: Random.date('yyyy-MM-dd'),
          comment: '提交报销申请'
        },
        {
          operator: Random.cname(),
          action: Random.pick(['approve', 'reject']),
          date: Random.date('yyyy-MM-dd'),
          comment: Random.csentence(5, 20)
        }
      ]])
    }
  }
}

/**
 * 生成财务统计数据
 */
const generateFinanceStats = () => {
  return {
    code: 200,
    message: 'success',
    data: {
      pending: Random.integer(0, 10),
      approved: Random.integer(10, 50),
      rejected: Random.integer(0, 10),
      total: Random.integer(50, 100)
    }
  }
}

/**
 * 设置财务模块Mock
 */
export function setupFinanceMock() {
  // 获取报销列表
  Mock.mock(/\/api\/finance\/reimbursement\/list/, 'get', (options) => {
    const url = options.url
    const query = {}
    url.replace(/\?(.+)/, function(_, search) {
      search.split('&').forEach(function(item) {
        const parts = item.split('=')
        query[parts[0]] = parts[1]
      })
    })
    return generateReimbursementList(query)
  })
  
  // 获取报销详情
  Mock.mock(/\/api\/finance\/reimbursement\/detail\/\w+/, 'get', (options) => {
    const id = options.url.match(/\/api\/finance\/reimbursement\/detail\/(\w+)/)[1]
    return generateReimbursementDetail(id)
  })
  
  // 提交报销申请
  Mock.mock('/api/finance/reimbursement/submit', 'post', () => {
    return {
      code: 200,
      message: 'success',
      data: {
        id: 'EXP' + Random.integer(10000, 99999),
        status: 'pending'
      }
    }
  })
  
  // 获取财务统计
  Mock.mock('/api/finance/stats', 'get', generateFinanceStats)
} 